<?php

require_once 'lib/dbappinclude.php';

/*====================================*/
/*      CHECKS FOR EXISTING USER      */
/*====================================*/
function getUser ($id)
{
	$sql = "SELECT user_id FROM user WHERE user_id = $id";
	$result = mysql_query($sql) or die(mysql_error());

	return mysql_num_rows($result);
}

/*====================================*/
/* ADDS A NEW USER INTO THE DATABASE  */
/*====================================*/
function newUser ($id, $name)
{
	$sql = "INSERT INTO user (user_id, user_name, user_1st, user_2nd, user_3rd, user_4th, user_all) " .
		   "VALUES ('$id', '$name', 0, 0, 0, 0, 0)";
	mysql_query($sql) or die(mysql_error());
}

/*====================================*/
/*  RETURNS XML STRING OF USER STATS  */
/*====================================*/
function getStats ($id)
{
	$query = "SELECT * FROM user WHERE user_id = $id";
	$result = mysql_query($query) or die(mysql_error());
	$row = mysql_fetch_array($result);

	$name = $row['user_name'];
	$s1 = $row['user_1st'];
	$s2 = $row['user_2nd'];
	$s3 = $row['user_3rd'];
	$s4 = $row['user_4th'];
	$sa = $row['user_all'];
	$pt = $row['user_pts'];
	$st = $row['user_st'];
	$fl = $row['user_fl'];
	$fh = $row['user_fh'];
	$fk = $row['user_fk'];
	$sf = $row['user_sf'];
	$dr = $row['user_dr'];

$xmlstr =<<<XML
<user>
  <id>$id</id>
  <name>$name</name>
  <stats>
    <s1>$s1</s1>
    <s2>$s2</s2>
    <s3>$s3</s3>
    <s4>$s4</s4>
    <sa>$sa</sa>
    <pt>$pt</pt>
    <st>$st</st>
    <fl>$fl</fl>
    <fh>$fh</fh>
    <fk>$fk</fk>
    <sf>$sf</sf>
    <dr>$dr</dr>
  </stats>
</user>
XML;

	return $xmlstr;
}

/*====================================*/
/*  INCREMENTS SEPECIFIED RANK COUNT  */
/*====================================*/
function plusRank ($id, $rank)
{
	if($rank == 1)
	{
		$query = "SELECT user_1st FROM user WHERE user_id = $id";
		$result = mysql_query($query) or die(mysql_error());
		$row = mysql_fetch_array($result);

		$temp = $row['user_1st']+1;

		$sql = "UPDATE user SET " .
			   "user_1st = '$temp' " .
			   "WHERE user_id = $id";
	}
	else if($rank == 2)
	{
		$query = "SELECT user_2nd FROM user WHERE user_id = $id";
		$result = mysql_query($query) or die(mysql_error());
		$row = mysql_fetch_array($result);

		$temp = $row['user_2nd']+1;

		$sql = "UPDATE user SET " .
			   "user_2nd = '$temp' " .
			   "WHERE user_id = $id";
	}
	else if($rank == 3)
	{
		$query = "SELECT user_3rd FROM user WHERE user_id = $id";
		$result = mysql_query($query) or die(mysql_error());
		$row = mysql_fetch_array($result);

		$temp = $row['user_3rd']+1;

		$sql = "UPDATE user SET " .
			   "user_3rd = '$temp' " .
			   "WHERE user_id = $id";
	}
	else if($rank == 4)
	{
		$query = "SELECT user_4th FROM user WHERE user_id = $id";
		$result = mysql_query($query) or die(mysql_error());
		$row = mysql_fetch_array($result);

		$temp = $row['user_4th']+1;

		$sql = "UPDATE user SET " .
			   "user_4th = '$temp' " .
			   "WHERE user_id = $id";
	}
	else
	{
		print "**ERROR**\r\n";
	}
	mysql_query($sql) or die(mysql_error());
}

/*====================================*/
/*    INCREMENTS TOTAL GAME COUNT     */
/*====================================*/
function plusGame ($id)
{
	$query = "SELECT user_all FROM user WHERE user_id = $id";
	$result = mysql_query($query) or die(mysql_error());
	$row = mysql_fetch_array($result);

	$temp = $row['user_all']+1;

	$sql = "UPDATE user SET " .
		   "user_all = '$temp' " .
		   "WHERE user_id = $id";
	mysql_query($sql) or die(mysql_error());
}

/*====================================*/
/* COMBO AND POINTS SET&GET FUNCTIONS */
/*====================================*/

/*------------------*/
/*      POINTS      */
/*------------------*/
function getPTS($id)
{
	$query = "SELECT user_pts FROM user WHERE user_id = $id";
	$result = mysql_query($query) or die(mysql_error());
	$row = mysql_fetch_array($result);

	$temp = $row['user_pts'];
	return $temp;
}

function plusPTS($id, $value)
{
	$total = getPTS($id) + $value;

	$sql = "UPDATE user SET " .
		   "user_pts = '$total' " .
		   "WHERE user_id = $id";
	mysql_query($sql) or die(mysql_error());
}

/*------------------*/
/*    STRAIGHTS     */
/*------------------*/
function getST($id)
{
	$query = "SELECT user_st FROM user WHERE user_id = $id";
	$result = mysql_query($query) or die(mysql_error());
	$row = mysql_fetch_array($result);

	$temp = $row['user_st'];
	return $temp;
}

function plusST($id, $value)
{
	$total = getST($id) + $value;

	$sql = "UPDATE user SET " .
		   "user_st = '$total' " .
		   "WHERE user_id = $id";
	mysql_query($sql) or die(mysql_error());
}

/*------------------*/
/*     FLUSHES      */
/*------------------*/
function getFL($id)
{
	$query = "SELECT user_fl FROM user WHERE user_id = $id";
	$result = mysql_query($query) or die(mysql_error());
	$row = mysql_fetch_array($result);

	$temp = $row['user_fl'];
	return $temp;
}

function plusFL($id, $value)
{
	$total = getFL($id) + $value;

	$sql = "UPDATE user SET " .
		   "user_fl = '$total' " .
		   "WHERE user_id = $id";
	mysql_query($sql) or die(mysql_error());
}

/*------------------*/
/*   FULL-HOUSES    */
/*------------------*/
function getFH($id)
{
	$query = "SELECT user_fh FROM user WHERE user_id = $id";
	$result = mysql_query($query) or die(mysql_error());
	$row = mysql_fetch_array($result);

	$temp = $row['user_fh'];
	return $temp;
}

function plusFH($id, $value)
{
	$total = getFH($id) + $value;

	$sql = "UPDATE user SET " .
		   "user_fh = '$total' " .
		   "WHERE user_id = $id";
	mysql_query($sql) or die(mysql_error());
}

/*------------------*/
/* FOUR-OF-A-KINDS  */
/*------------------*/
function getFK($id)
{
	$query = "SELECT user_fk FROM user WHERE user_id = $id";
	$result = mysql_query($query) or die(mysql_error());
	$row = mysql_fetch_array($result);

	$temp = $row['user_fk'];
	return $temp;
}

function plusFK($id, $value)
{
	$total = getFK($id) + $value;

	$sql = "UPDATE user SET " .
		   "user_fk = '$total' " .
		   "WHERE user_id = $id";
	mysql_query($sql) or die(mysql_error());
}

/*------------------*/
/* STRAIGHT-FLUSHES */
/*------------------*/
function getSF($id)
{
	$query = "SELECT user_sf FROM user WHERE user_id = $id";
	$result = mysql_query($query) or die(mysql_error());
	$row = mysql_fetch_array($result);

	$temp = $row['user_sf'];
	return $temp;
}

function plusSF($id, $value)
{
	$total = getSF($id) + $value;

	$sql = "UPDATE user SET " .
		   "user_sf = '$total' " .
		   "WHERE user_id = $id";
	mysql_query($sql) or die(mysql_error());
}

/*------------------*/
/*      DRAGON      */
/*------------------*/
function getDR($id)
{
	$query = "SELECT user_dr FROM user WHERE user_id = $id";
	$result = mysql_query($query) or die(mysql_error());
	$row = mysql_fetch_array($result);

	$temp = $row['user_dr'];
	return $temp;
}

function plusDR($id, $value)
{
	$total = getDR($id) + $value;

	$sql = "UPDATE user SET " .
		   "user_dr = '$total' " .
		   "WHERE user_id = $id";
	mysql_query($sql) or die(mysql_error());
}

?>